<?php
/**
 * Created by PhpStorm.
 * User: XD
 * Date: 2017/10/23
 * Time: 10:41
 */
namespace App\Repositories\Email;

use App\Models\Email\Task;
use App\Models\Email\Task_Asin;
use App\Models\Email\Template;

class EmailTaskRespository
{
    protected $task;

    public function __construct(Task $task)
    {
        $this->task = $task;
    }

    /**
     * 通过templateid获取 taskname
     * @param $id
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getTaskByTemplateId($id)
    {
        return $this->task->where('templateid', $id)->get(['taskname']);
    }


    public function saveTask($all)
    {

        $taskId = \DB::connection('mysql-41')->select('select NEXT VALUE FOR MYCATSEQ_GLOBAL as id')[0]->id;
        $this->task->id = $taskId;
        $this->task->taskname = $all['taskname'];
        $this->task->status = $all['status'];
        $this->task->templateid = $all['templateid'];
        $this->task->orderModel = $all['orderModel'];
        $this->task->orderTimeRange = $all['orderTimeRange'];
        $this->task->fixedTimeRange = $all['fixedTimeRange'];
        $this->task->sendStart = $all['sendStart'];
        $this->task->sendEnd = $all['sendEnd'];
        $this->task->groupid = $all['groupid'];
        $this->task->conditionTag = $all['conditionTag'];
        $this->task->AppName = $all['AppName'];
        foreach ($all['Asin'] as $asinId) {
            //对Asin进行绑定,多对多的关系。
            $taskasinId = \DB::connection('mysql-41')->select('select NEXT VALUE FOR MYCATSEQ_GLOBAL as id')[0]->id;
            $taskAsin = new Task_Asin();
            $taskAsin->id = $taskasinId;
            $taskAsin->taskId = $taskId;
            $taskAsin->asinId = $asinId;
            $taskAsin->save();
        }
        $Remark = $all['Remark'];
        if ($Remark != null) {
            $this->task->Remark = $Remark;
        }
        if ($this->task->save()) {
            return true;
        };
    }

    public function changetask($all)
    {
        if (!array_key_exists('taskId', $all)) {
            return false;
        }
        $taskId = $all['taskId'];
        $this->task = $this->task->find($taskId);
        $this->task->taskname = $all['taskname'];
        $this->task->status = $all['status'];
        $this->task->templateid = $all['templateid'];
        $this->task->orderModel = $all['orderModel'];
        $this->task->orderTimeRange = $all['orderTimeRange'];
        $this->task->fixedTimeRange = $all['fixedTimeRange'];
        $this->task->sendStart = $all['sendStart'];
        $this->task->sendEnd = $all['sendEnd'];
        $this->task->groupid = $all['groupid'];
        $this->task->conditionTag = $all['conditionTag'];
        $this->task->AppName = $all['AppName'];
        $task_Asin = new Task_Asin();
        $task_Asins = $task_Asin->where('taskId', $taskId)->get();
        foreach ($task_Asins as $task_Asin) {
            Task_Asin::destroy($task_Asin->id);
        }
        foreach ($all['Asin'] as $asinId) {
            //对Asin进行绑定,多对多的关系。
            $taskasinId = \DB::connection('mysql-41')->select('select NEXT VALUE FOR MYCATSEQ_GLOBAL as id')[0]->id;
            $taskAsin = new Task_Asin();
            $taskAsin->id = $taskasinId;
            $taskAsin->taskId = $taskId;
            $taskAsin->asinId = $asinId;
            $taskAsin->save();
        }
        $Remark = $all['Remark'];
        if ($Remark != null) {
            $this->task->Remark = $Remark;
        }
        if ($this->task->save()) {
            return true;
        };
    }

    public function getTasks($page, $pageNum)
    {
        $tasks = $this->task->limit($pageNum)->offset($page * $pageNum)->get();
        $tasksArray = array();
        $i = 0;
        foreach ($tasks as $task) {
            $tasksArray['task'][$i] = $task;
            $tasksArray['task'][$i]['asin'];
            $tempalte = new Template();
            $tempalte = $tempalte->find($task->templateid);
            $templateNmae = $tempalte->name;
            $tasksArray['task'][$i]['templateName'] = $templateNmae;
            $asins = $this->task->find($task->id)->asins()->get();
            $tempAsin = array();
            foreach ($asins as $asin) {
                $tempAsin[] = $asin;
            }
            $tasksArray['task'][$i]['asin'] = $tempAsin;
            $i++;
        }
        return $tasksArray;
    }

    /**
     * @param $taskName
     * @param $groupid
     * @param $status
     * @param $pageNum
     * @param $page
     * @return array
     */
    public function searchtask($taskName, $groupid, $status, $pageNum, $page)
    {
        $task = $this->task;
        $tasks = $task->where('taskname', 'like', '%' . $taskName . '%')->where('groupid', '=', $groupid)->where('status', $status)->limit($pageNum)->offset($pageNum * $page)->get();
//
        //取到真是的tasks，获取每个tasks的asins
        $tasksArrayReturn = array();
        $i = 0;
        foreach ($tasks as $task) {
            $tasksArrayReturn['task'][$i] = $task;
            $tasksArrayReturn['task'][$i]['asin'];
            $tempalte = new Template();
            $tempalte = $tempalte->find($task->templateid);
            $templateName = $tempalte->name;
            $tasksArrayReturn['task'][$i]['templateName'] = $templateName;
            $asins = $this->task->find($task->id)->asins()->get();
            $tempAsinReturn = array();
            foreach ($asins as $asin) {
                $tempAsinReturn[] = $asin;
            }
            $tasksArrayReturn['task'][$i]['asin'] = $tempAsinReturn;
            $i++;
        }
        return $tasksArrayReturn;
    }

    public function listTasks()
    {
        return $this->task->where('status', 1)->get();
    }





}